
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_blocked_users.
	''' </summary>
	Public Partial Class view_blocked_users
		Inherits BaseWebPage

		Protected litUsersCount As System.Web.UI.WebControls.Literal
		Protected blankImagesCount As Integer = 0
		Protected currentPageNumber As Integer = 0
		Protected firstPageNumber As Integer = 0
		' the first page of page links
		Protected lastPageNumber As Integer = 0
		Protected pagesRest As Integer = 0
		Protected usrID As Integer = -1


		Public Function CreateDataSource() As ICollection

			Dim usr As User = Users.GetUserByID(usrID)

			Dim startIndex As Integer = Convert.ToInt32(txtStartIndexHidden.Text)
			Dim endIndex As Integer = startIndex + Constants.MAX_PICTURES_ON_PAGE
			Dim i As Integer
			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("ImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Name", GetType(String)))
			dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
			dt.Columns.Add(New DataColumn("UnblockUserVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("UserID", GetType(String)))
			dt.Columns.Add(New DataColumn("FullName", GetType(String)))

			Dim temp As UserPicture
			Dim blockedUsersCount As Integer = usr.BlockedUsers.Count
			Dim imagesOnLastRow As Integer = 0
			Dim t As Integer = 0
			Dim counter As Integer = 0

			endIndex = Math.Min(endIndex, blockedUsersCount)
			t = (endIndex - startIndex)
			Math.DivRem(t, Constants.MAX_PICTURES_ON_ROW, imagesOnLastRow)
			dgPager.dgListing.StartIndex = startIndex + 1
			dgPager.dgListing.EndIndex = endIndex
			dgPagerUp.dgListing.StartIndex = startIndex + 1
			dgPagerUp.dgListing.EndIndex = endIndex

			counter = 0
			i = 0
			Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

			For Each bu As BlockedUser In usr.BlockedUsers
				If counter >= startIndex AndAlso counter < endIndex Then
					dr = dt.NewRow()
					temp = bu.BlockedUsr.DefaultPicture

					If temp <> Nothing Then
						dr(0) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(0) = "../images/default_thumbnail.gif"
					End If
					dr(1) = getBeginning(HttpUtility.HtmlEncode(bu.BlockedUsr.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					If LoggedUsersList.Contains(bu.BlockedUsr.ID) Then
						dr(2) = "../images/online_status.gif"
					Else
						dr(2) = "../images/spacer.gif"
						' init according to the friend's online status
					End If
					dr(3) = True
					dr(4) = bu.BlockedUsr.ID
					dr(5) = HttpUtility.HtmlEncode(bu.BlockedUsr.Name)
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
			Next

			If imagesOnLastRow <> 0 Then
				blankImagesCount = Constants.MAX_PICTURES_ON_ROW - imagesOnLastRow
				i = imagesOnLastRow
				While i < Constants.MAX_PICTURES_ON_ROW
					dr = dt.NewRow()
					'dr[0] = "../images/empty_image.gif";
					dr(0) = "../images/spacer.gif"
					dr(1) = ""
					dr(2) = "../images/spacer.gif"
					dr(3) = "false"
					dt.Rows.Add(dr)
					System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
				End While
			End If

			Dim dv As New DataView(dt)
			Return dv
		End Function


		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.VIEW_BLOCKED_USERS_PAGE;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.VIEW_BLOCKED_USERS, "", _Form1, Nothing, Nothing)
			Me.initAdvertisements(_Form1, Nothing)

			usrID = usr.ID
			Dim blockedUsersCount As Double = Convert.ToDouble(usr.BlockedUsers.Count)
			If blockedUsersCount = 0 Then
				lblNoBlockedUsers.Visible = True
				tblBlockedUsers.Visible = False
			Else

				dgPager.RedirectPage = Constants.VIEW_BLOCKED_USERS
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(blockedUsersCount / Constants.MAX_PICTURES_ON_PAGE))
				dgPager.Action = ""
				dgPagerUp.RedirectPage = Constants.VIEW_BLOCKED_USERS
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(blockedUsersCount / Constants.MAX_PICTURES_ON_PAGE))
				dgPagerUp.Action = ""

				' initialize listing literals

				dgPager.dgListing.TotalCount = Convert.ToInt32(blockedUsersCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(blockedUsersCount)

				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					txtStartIndexHidden.Text = "0"
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					txtStartIndexHidden.Text = Convert.ToString(((dgPager.CurrentPageNumber - 1) * Constants.MAX_PICTURES_ON_PAGE))
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)

				If Not IsPostBack Then
					BindList()
				End If
			End If
		End Sub

		Public Sub BindList()
			dlBlockedUsers.DataSource = CreateDataSource()
			dlBlockedUsers.DataBind()

			Dim itemsCount As Integer = dlBlockedUsers.Items.Count
			Dim blankImageIndex As Integer = itemsCount - blankImagesCount
			Dim i As Integer = 0
			i = blankImageIndex
			While i < itemsCount
				(DirectCast(dlBlockedUsers.Items(i).FindControl("lbName"), System.Web.UI.WebControls.LinkButton)).CssClass = "empty_image"
				(DirectCast(dlBlockedUsers.Items(i).FindControl("imgPhoto"), System.Web.UI.WebControls.ImageButton)).CssClass = "empty_image"
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Private Sub dlBlockedUsers_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
			Dim src As Object = e.CommandSource
			Dim srcType As String = src.[GetType]().ToString()
			Dim cmd As String = e.CommandName
			Dim arg As String = ""
			If srcType.Equals("System.Web.UI.WebControls.ImageButton") Then
				arg = (DirectCast(src, ImageButton)).CommandArgument
			Else
				arg = (DirectCast(src, LinkButton)).CommandArgument
			End If
			Select Case cmd
				Case "UnblockUser"
					Dim usr As BO.User = LoggedUser
					usr.BlockedUsers.Remove(Convert.ToInt32(arg))
					Response.Redirect(Constants.VIEW_BLOCKED_USERS)
					Exit Select
				Case "MemberRedirect"
					Response.Redirect(Constants.MEMBERS + "?id=" + e.CommandArgument)
					Exit Select
			End Select
		End Sub
		Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
			Dim stringBuilder As New StringBuilder()
			Dim stringWriter As New StringWriter(stringBuilder)
			Dim htmlWriter As New HtmlTextWriter(stringWriter)
			MyBase.Render(htmlWriter)
			Dim html As String = stringBuilder.ToString()
			Dim start As Integer = html.IndexOf("<form name=""") + 12
			Dim [end] As Integer = html.IndexOf("""", start)
			Dim formID As String = html.Substring(start, [end] - start)
			Dim replace As String = formID.Replace(":", "_")
			html = html.Replace("document." + formID, "document." + replace)
			writer.Write(html)
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlBlockedUsers.ItemCommand, AddressOf Me.dlBlockedUsers_ItemCommand
		End Sub
#End Region

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable _Form1.
		====================
		Public ReadOnly Property _Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated__Form1
			End Get
		End Property
		'set { Migrated__Form1 = value; }
	End Class
End Namespace






